package it.aekidna.cleverplatypus.gui.components.columnsbrowser
{
	import flash.events.Event;
	import flash.events.EventDispatcher;

	import it.aekidna.cleverplatypus.utils.ArrayCollectionUtils;

	import mx.collections.ArrayCollection;
	import mx.core.IFactory;

	[Bindable]
	public class ColumnsBrowserColumnPM extends EventDispatcher
	{
		private var _dataProvider : ArrayCollection;

		public var level : int = 0;

		private var _selectedItems : ArrayCollection = new ArrayCollection();

		public var itemRenderer : IFactory;

		public var parent : ColumnsBrowserColumnPM;
		public var child : ColumnsBrowserColumnPM;
		public var pModel : ColumnsBrowserPM;


		public function ColumnsBrowserColumnPM( inParent : ColumnsBrowserColumnPM, inModel : ColumnsBrowserPM )
		{
			_dataProvider = new ArrayCollection();
			_dataProvider.filterFunction = filterFunction;
			parent = inParent;
			pModel = inModel;
			addEventListener( "filterChange", onFilterChange );
		}


		private function onFilterChange( inEvent : Event ) : void
		{
			if( parent )
			{
				parent.dispatchEvent( inEvent.clone());
			}
		}


		public function get dataProvider() : ArrayCollection
		{
			return _dataProvider;
		}


		public function set dataProvider( inDataProvider : ArrayCollection ) : void
		{
			_dataProvider.filterFunction = null;
			_dataProvider.refresh();

			//ArrayCollectionUtils.setItemsFromList( _dataProvider, inDataProvider );
			if( inDataProvider )
				_dataProvider = inDataProvider;
			else
				_dataProvider.removeAll();

			_dataProvider.filterFunction = filterFunction;
			_dataProvider.refresh();
		}


		public function set selectedItems( inItems : Object ) : void
		{
			if( inItems is Array || inItems is ArrayCollection || inItems == null )
			{
				ArrayCollectionUtils.setItemsFromList( _selectedItems, inItems );
				pModel.selectedItems = _selectedItems;
			}

			if( child )
			{
				for each( var filter : Filter in child.filters )
				{
					if( filter.recursive && !hasFilter( filter.name ))
						child.removeNamedFilter( filter.name );
				}
			}
		}


		public function get selectedItems() : Object
		{
			return _selectedItems;
		}

		private var _filters : Object = {};


		private function filterFunction( inObject : Object ) : Boolean
		{
			for each( var filter : Filter in _filters )
			{
				if( filter.filterFunction( inObject ) == false )
					return false;
			}
			return true;
		}


		public function setFilter( inFilter : Filter ) : void
		{
			_filters[ inFilter.name ] = inFilter;
			_dataProvider.refresh();

			if( inFilter.recursive && child )
				child.setFilter( inFilter );
			dispatchEvent( new OriginalTargetEvent( "filterChange", this ));
		}


		public function removeNamedFilter( inName : String ) : void
		{
			if( _filters.hasOwnProperty( inName ))
			{
				delete _filters[ inName ];
				_dataProvider.refresh();

				if( child )
					child.removeNamedFilter( inName );
				dispatchEvent( new OriginalTargetEvent( "filterChange", this ));
			}
		}


		public function hasFilter( inName : String ) : Boolean
		{
			return _filters.hasOwnProperty( inName );
		}


		public function get filters() : Array
		{
			var outFilters : Array = [];

			for each( var filter : Filter in _filters )
				outFilters.push( filter );
			return outFilters;
		}


	}
}
